﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Security.Cryptography;
using System.Windows.Shapes;

namespace RandomStudent.Extends
{
    public class CanvasExtend : Canvas
    {
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            int angle = 30;

            //画12个板块。。
            int count = 12;

            // angle*cout==360 .这个是必须的。。不要乱设置
       
            for (int i = 0; i < count; i++)
            {
                Path path = new Path();
                path.Stroke = System.Windows.Media.Brushes.Black;

                LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
                myLinearGradientBrush.StartPoint = new Point(0, 0);
                myLinearGradientBrush.EndPoint = new Point(1, 1);
                myLinearGradientBrush.GradientStops.Add(
                    new GradientStop(GetRandomColor(), 0.0));

                double random = new Random().NextDouble();
                myLinearGradientBrush.GradientStops.Add(
                    new GradientStop(GetRandomColor(), random));
                myLinearGradientBrush.GradientStops.Add(
                    new GradientStop(GetRandomColor(), 1 - random));
                myLinearGradientBrush.GradientStops.Add(
                    new GradientStop(GetRandomColor(), 1.0));


                path.Fill = myLinearGradientBrush;
                path.StrokeThickness = 1;
                path.HorizontalAlignment = HorizontalAlignment.Left;
                path.VerticalAlignment = VerticalAlignment.Center;
                PathGeometry pathGeomerty = new PathGeometry();
                pathGeomerty.Figures.Add(GetFigure(i * angle, angle));
                dc.DrawGeometry(path.Fill, new Pen(Brushes.Black, 1), pathGeomerty);             
            }
        }

        private Color GetRandomColor()
        {
            return Color.FromArgb((byte)RollDice(266), (byte)RollDice(266), (byte)RollDice(266), (byte)RollDice(266));
        }

        /// <summary>
        /// 掷随即数
        /// </summary>
        /// <param name="NumSides">最大数，返回的结果不包括这个数，从0开始</param>
        /// <returns></returns>
        public static int RollDice(int NumSides)
        {
            // Create a byte array to hold the random value.
            byte[] randomNumber = new byte[1];

            // Create a new instance of the RNGCryptoServiceProvider.
            RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();

            // Fill the array with a random value.
            Gen.GetBytes(randomNumber);

            // Convert the byte to an integer value to make the modulus operation easier.
            int rand = Convert.ToInt32(randomNumber[0]);

            // Return the random number mod the number
            // of sides.  The possible values are zero-
            // based, so we add one.
            return rand % NumSides;
        }

        double centerX = 100;
        double centerY = 100;

        /// <summary>
        /// 返回轮廓
        /// </summary>
        /// <param name="angleStart">开始角度</param>
        /// <param name="angleEnd">结束角度</param>
        /// <returns></returns>
        public PathFigure GetFigure(double angleStart, double angleEnd)
        {
            centerX = Width / 2;
            centerY = Height / 2;
            double innerRadius = 30;
            double Radius = centerX;
            double angle = angleStart;
            double WedgeAngle = angleEnd;

            PathFigure pathFigure = new PathFigure();


            Point innerArcStartPoint = ComputeCartesianCoordinate(angle, innerRadius);
            Point innerArcEndPoint = ComputeCartesianCoordinate(angle + WedgeAngle, innerRadius);

            Point outerArcStartPoint = ComputeCartesianCoordinate(angle, Radius);
            Point outerArcEndPoint = ComputeCartesianCoordinate(angle + WedgeAngle, Radius);

            outerArcStartPoint.Offset(centerX, centerY);
            outerArcEndPoint.Offset(centerX, centerY);

            innerArcStartPoint.Offset(centerX, centerY);
            innerArcEndPoint.Offset(centerX, centerY);
            LineSegment lineToOuterArcStartPoint = new LineSegment(outerArcStartPoint, true);
            ArcSegment arcToOuterArcEndPoint = new ArcSegment(outerArcEndPoint, new Size(Radius, Radius), 0, false, SweepDirection.Clockwise, true);
            LineSegment lineToInnerArcEndPoint = new LineSegment(innerArcEndPoint, false);
            ArcSegment arcToinnerAcsStartPoint = new ArcSegment(innerArcStartPoint, new Size(innerRadius, innerRadius), 0, false, SweepDirection.Counterclockwise, true);

            pathFigure.StartPoint = innerArcStartPoint;
            pathFigure.Segments.Add(lineToOuterArcStartPoint);
            pathFigure.Segments.Add(arcToOuterArcEndPoint);
            pathFigure.Segments.Add(lineToInnerArcEndPoint);
            pathFigure.Segments.Add(arcToinnerAcsStartPoint);

            return pathFigure;
        }

        /// <summary>
        /// 把角度转换成幅度坐标,返回的角度是用正上方开始为0度，向右旋转，和数学的不一样
        /// </summary>
        /// <param name="angle">角度</param>
        /// <param name="radius">半径</param>
        /// <returns></returns>
        public static Point ComputeCartesianCoordinate(double angle, double radius)
        {
            double angleRad = (Math.PI / 180.0) * (angle - 90);

            double x = radius * Math.Cos(angleRad);
            double y = radius * Math.Sin(angleRad);

            return new Point(x, y);
        }
    }
}
